'use strict';

const matrix = [
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9, 10, 11, 12],
];

/**
 * @param {number[][]} matrix
 * @return {number[]}
 */
var spiralOrder = function (matrix) {
  const result = [];
  if (matrix.length === 0) return result;

  let startRow = 0,
    endRow = matrix.length - 1,
    startCol = 0,
    endCol = matrix[0].length - 1;

  while (startRow <= endRow && startCol <= endCol) {
    for (let col = startCol; col <= endCol; col++) {
      result.push(matrix[startRow][col]);
    }
    startRow++;

    for (let row = startRow; row <= endRow; row++) {
      result.push(matrix[row][endCol]);
    }
    endCol--;

    if (startRow <= endRow) {
      for (let col = endCol; col >= startCol; col--) {
        result.push(matrix[endRow][col]);
      }
      endRow--;
    }

    if (startCol <= endCol) {
      for (let row = endRow; row >= startRow; row--) {
        result.push(matrix[row][startCol]);
      }
      startCol++;
    }
  }

  return result;
};

console.log(spiralOrder(matrix)); // [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]
